package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.IcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private IcartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart( @Valid @RequestBody Cart cart) {
        cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCartList() {
        return ResponseEntity.ok(cartService.queryCartList());
    }



    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("id")Long id,@RequestParam("num")Integer num){
        cartService.updateNum(id,num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId")String skuId){
        cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }


    @PostMapping("list")
    private ResponseEntity<Void> addCartList(@RequestBody List<Cart> carts){
        cartService.addCartList(carts);
        return ResponseEntity.ok().build();
    }
}
